package com.press.services;

import com.press.bean.ContentBean;
import com.press.bean.TaxonomyBean;
import com.press.beanforview.VContentBean;
import com.press.mapper.SearchMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * Created by dana on 2016/10/19.
 */
@Service("searchService")
public class SearchService {
    @Resource(name="searchMapper")
    private SearchMapper searchMapper;


    /**
     * 根据关键词searchcontent搜索文章概要
     * 查询startcount 之后的6条数据
     * @param search
     * @param pn
     * @return
     */
    public List<ContentBean> searchfromsummarypage(String search, String pn){
        search="%"+search+"%";
        if(pn==null)
            pn=String.valueOf(1);
        return searchMapper.searchfromsummarypage(search,(Integer.valueOf(pn)-1)*6);
    }

    public int getcountforsearch(String searchcontent){
        searchcontent="%"+searchcontent+"%";
        return searchMapper.getcountforsearch(searchcontent);
    }

    /**
     * 通过文章id得到文章分类名称和分类id
     * @return
     */
    public List<TaxonomyBean> gettaxonomybycontent_id(String content_id){
        return searchMapper.gettaxonomybycontent_id(content_id);
    }

    /**
     * 该方法用于把ContentBean 和 Taxonomy存放到VContentBeanh中
     * @param contents
     * @param taxonomys
     * @return
     */
    public List<VContentBean> set_cb_tb_invb(List<ContentBean> contents,List<TaxonomyBean> taxonomys,String url){
        List<VContentBean> result = new ArrayList<>();
        for(int i=0;i<contents.size();i++){
            VContentBean vcontent=new VContentBean();
            vcontent.setAuthor(contents.get(i).getContent_author());
            vcontent.setContent_type("noBin");
            vcontent.setCreateDate(contents.get(i).getContent_created());
            vcontent.setModifiedDate(contents.get(i).getContent_modified());
            vcontent.setSummary(contents.get(i).getContent_summary());
            vcontent.setText(contents.get(i).getContent_text());
            vcontent.setTitle(contents.get(i).getContent_title());
            vcontent.setThumbnail(contents.get(i).getContent_thumbnail());
            vcontent.setWatched(contents.get(i).getContent_watched());
            vcontent.setUrl(url+"/content?content_id="+contents.get(i).getContent_id());
            Map<String,String> taxonomy = new LinkedHashMap<>();
            for(int j = 0 ; j < taxonomys.size();j++){
                if(taxonomys.get(j).getTaxonomy_type().equals(contents.get(i).getContent_id())){
                    taxonomy.put(url+"/taxonomy?tax="+taxonomys.get(j).getTaxonomy_id(),taxonomys.get(j).getTaxonomy_title());
                }
            }
            vcontent.setTaxonomys(taxonomy);
            result.add(vcontent);
        }
        return result;
    }

    public SearchMapper getSearchMapper() {
        return searchMapper;
    }

    public void setSearchMapper(SearchMapper searchMapper) {
        this.searchMapper = searchMapper;
    }
}
